/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.loan;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import src.domain.loan.Loan;
import src.domain.saving.Saving;
import src.domain.security.Users;
import src.services.gl.GlaccntModel;
import src.services.loan.*;
import src.services.saving.SavingModel;
import src.util.Constanta;
import src.util.Messager;
import src.util.Utils;
import src.view.security.LoginBean;
import src.view.system.ApprovalBean;

/**
 *
 * @author jasuke
 */
@ManagedBean
@SessionScoped
public class LoanddApprovalBean {

    private List<SelectItem> avaibleCoral, availableGL, avblMarketing;
    private List<SelectItem> availableFee, avblPoint;
    private List<Loan> loans;
    
    private Loan loan;
    private LoanModel helper;
    private boolean editMode = true;
    private boolean success; 
    private Users users;      
    private ApprovalBean approvalBean;
    private String disbAccnt, disbAccntOwner;
    private String autodebitAccnt, autodebitAccntOwner;
    
    public LoanddApprovalBean() {
        ambilUser();
        this.helper = new LoanModel();
        this.availableFee = new LoanfeeModel().listAll();
        this.avblPoint = new PaypointModel().listAll();
        FacesContext context = FacesContext.getCurrentInstance();
        approvalBean = (ApprovalBean) context.getApplication().evaluateExpressionGet(context, "#{approvalBean}", ApprovalBean.class);
        approvalBean.setUpdateTab(false);
    }
    
    public Loan getLoan() {
        return loan;
    }

    public void setLoan(Loan loan) {
        this.loan = loan;
    }

    public List<SelectItem> getAvaibleCoral() {
        return avaibleCoral;
    }

    public List<SelectItem> getAvailableGL() {
        return availableGL;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public List<Loan> getLoans() {
        return loans;
    }

    public String selectItemValueOf(List<SelectItem> avblList, Integer id) {
        return (String) Utils.selectItemValueOf(avblList, id);
    }

    public String getAutodebitAccnt() {
        return autodebitAccnt;
    }

    public String getAutodebitAccntOwner() {
        return autodebitAccntOwner;
    }

    public String getDisbAccnt() {
        return disbAccnt;
    }

    public String getDisbAccntOwner() {
        return disbAccntOwner;
    }
    
    private void initForm() {
        this.avaibleCoral = new CollateralModel().listAll();
        this.availableGL = new GlaccntModel().listAll();
        this.avblMarketing = new MarketingModel().listAll();
        this.editMode = true;
    }
      
    public void openRuning() {  
        approvalBean.setUpdateTab(false);
        initForm();
        if (loan.getDisbursementSavaccntId() != null) {
            Saving saving = new SavingModel().getById(loan.getDisbursementSavaccntId());
            this.disbAccnt=saving.getAccountNo();
            this.disbAccntOwner=saving.getCustomer().getCustomerName(); 
        }
            
        if (loan.getLoanAutodebitSavingId() != null) {
            Saving saving = new SavingModel().getById(loan.getLoanAutodebitSavingId());
            this.autodebitAccnt=saving.getAccountNo();
            this.autodebitAccntOwner=saving.getCustomer().getCustomerName(); 
        }
    }
    
    public void getInterestRate() {  
        Integer prodId = loan.getLoanprod().getLoanprodId();
        BigDecimal amount = loan.getPrincipalAmount();
        
        if(prodId != null && amount != null) {
            BigDecimal intrate = new LoanprodModel().getInterestRate(loan.getLoanprod().getLoanprodId(), loan.getPrincipalAmount());
            loan.setInterestRate(intrate); 
        } else loan.setInterestRate(null); 
    }  
    
    public final void loadLoan() {
        Map<String, Object> savfilter = new HashMap<>();
        savfilter.put("loanStatus", Constanta.LOANSTATUS_APPROVED);
        this.loans = helper.findAllAdvance(savfilter, " order by loanNo");
    }
    
    public void approve(boolean approved) {
        RequestContext context = RequestContext.getCurrentInstance();
         
        if (approved==true) {
        
            loan.setLoanStatus(Constanta.LOANSTATUS_RUNNING);
            loan.setOwningAmount(loan.getPrincipalAmount());
            loan.setApprovalDate(Calendar.getInstance().getTime());
            success = helper.approve(loan);
            
        }else success=true;
                
        editMode = !success;        
        Messager.showSaveUpdateStatus(success);     
        context.addCallbackParam("success", success);  
    }
    
    private void ambilUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean logBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        this.users = logBean.getSelectedUser();
    }

    public List<SelectItem> getAvailableFee() {
        return availableFee;
    }

    public List<SelectItem> getAvblMarketing() {
        return avblMarketing;
    }

    public List<SelectItem> getAvblPoint() {
        return avblPoint;
    }
    
    public void handleClose() {
        if (success) {
            this.loans.remove(this.loan);
            this.loan = null;
            approvalBean.updateTabOtor();
        } 
    }
    
}
